package cn.edu.neusoft.wuguangsheng.market.activity;

import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import cn.edu.neusoft.wuguangsheng.market.R;
import cn.edu.neusoft.wuguangsheng.market.bean.RegisterBean;
import cn.edu.neusoft.wuguangsheng.market.listener.TListener;
import cn.edu.neusoft.wuguangsheng.market.model.RegisterModel;

public class RegisterActivity extends BaseActivity {
    EditText username1;
    EditText password1;
    EditText password2;
    EditText phone;
    EditText address;
    Button zc,fh;
    //ImageView fh;
    String success;
    String namer;
    String psdr;
    ImageView rt_look1,rt_look2;


    @Override
    void initView() {

        setLayout_file(R.layout.activity_register);
        username1=(EditText)findViewById(R.id.username1);
        password1=(EditText)findViewById(R.id.password1);
        password2=(EditText)findViewById(R.id.password2);
        phone=(EditText)findViewById(R.id.phone);
        address=(EditText)findViewById(R.id.address);
        zc=(Button)findViewById(R.id.register1);
        fh=(Button) findViewById(R.id.remove1);
        rt_look1=(ImageView)findViewById(R.id.rt_look1);
        rt_look2=(ImageView)findViewById(R.id.rt_look2);
    }

    @Override
    void initEvent() {
        zc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tmpuser = username1.getText().toString();
                String tmppass1 = password1.getText().toString().trim();
                String tmppass2 = password2.getText().toString().trim();
                String  tmpphone = phone.getText().toString();
                String  tmpaddress = address.getText().toString();

                if(tmpuser.equals("")||tmppass1.equals("")||tmppass2.equals("")||tmpphone.equals("")||tmpaddress.equals("")){
                    Toast.makeText(RegisterActivity.this,"输入信息不能为空",Toast.LENGTH_SHORT).show();
                }
                else if(!tmppass1.equals(tmppass2)){
                    Toast.makeText(RegisterActivity.this,"两次密码输入不一致",Toast.LENGTH_SHORT).show();
                }
                else if(!isMobileNO(tmpphone)){
                    Toast.makeText(RegisterActivity.this,"检查手机号码是否正确",Toast.LENGTH_SHORT).show();
                }
                else{
                    RegisterModel registerModel=new RegisterModel();
                    registerModel.getReg(tmpuser,tmppass1,tmpphone,tmpaddress,tListener);
                }
            }
        });

        fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        rt_look1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPasswordVisibility();
            }
        });
        rt_look2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setPasswordVisibility1();
            }
        });
    }

    @Override
    void initDate() {

    }



    TListener<RegisterBean> tListener=new TListener<RegisterBean>() {
        @Override
        public void onResponse(RegisterBean registerBean) {
            success=registerBean.getSuccess();
            if(success.equals("1")){
            Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                jump();
            }
            else if(success.equals("0")){
                Toast.makeText(RegisterActivity.this,"用户名已被注册",Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(RegisterActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };



    public void getUserinfo(){
        String namer=username1.getText().toString();
        String psdr=password1.getText().toString();
    }
    public void jump(){

        getUserinfo();
        Intent intent1=new Intent(RegisterActivity.this,LoginActivity.class);
        intent1.putExtra("REGNAME",namer);
        intent1.putExtra("REGPASS",psdr);
        setResult(RESULT_OK,intent1);
       finish();
    }

    /**
     * 设置密码可见和不可见的相互转换
     */
    private void setPasswordVisibility() {
        if (rt_look1.isSelected()) {
            rt_look1.setSelected(false);
            //密码不可见
            password1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        } else {
            rt_look1.setSelected(true);
            //密码可见
            password1.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }

    }

    private void setPasswordVisibility1() {
        if (rt_look2.isSelected()) {
            rt_look2.setSelected(false);
            //密码不可见
            password2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        } else {
            rt_look2.setSelected(true);
            //密码可见
            password2.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }

    }

    public static boolean isMobileNO(String mobiles) {
        //"[1]"代表第1位为数字1，"[358]"代表第二位可以为3、5、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。
        String telRegex = "[1][34578]\\d{9}" ;
        if (TextUtils.isEmpty(mobiles)) return false ;
        else return mobiles.matches( telRegex ) ;
    }
}